package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String, String> map) {
        //接收参数
        String phone = map.get("phone");
        //调用service保存
        userService.sendMsg(phone);
        return ResultInfo.success("发送短信成功");
    }

    //登录
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        //获取参数
        String phone = map.get("phone");
        String code = map.get("code");
        //调用service
       ResultInfo resultInfo= userService.login(phone, code);
        return resultInfo;
    }
    //app退出登录
    @PostMapping("/user/logout")
    public ResultInfo logout(@RequestHeader("Authorization")String token) {
        token = token.replace("Bearer", "").trim();
        userService.delete(token);
        return ResultInfo.success(null);
    }
}
